/*
 * Copyright 2018 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */

package com.zhongan.cloud.gateway.filter;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 熔断器过滤器。
 *
 * @author tongyufu
 */
@RestController
@Configuration
public class HystrixFilter {

    @Bean
    public RouteLocator hystrixRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
        //监听的地址，目标服务超时、停止都会触发熔断。恢复服务后大约有一分钟延迟才能被网关识别。
                .route(r -> r.path("/hystrix/**").filters(f -> f.hystrix(
                //熔断器名称
                        c -> c.setName("justAName")
                        //发生熔断时转发的地址
                                .setFallbackUri("forward:/hystrixfallback")))
                //目标服务
                        .uri("lb://HELLO-SERVICE")).build();
    }

    @RequestMapping("hystrixfallback")
    public String hystrixfallback() {
        System.out.println("======hystrixfallback");
        return "触发了熔断";
    }

}
